<template>
  <div class="sub-category">
    <div class="container" v-if="subCategoryData">
      <!-- 面包屑区域 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${subCategoryData.parentId}`">
          {{ subCategoryData.parentName }}
        </XtxBreadItem>
        <XtxBreadItem>{{ subCategoryData.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 筛选区域 -->
      <div class="sub-filter">
        <!-- 品牌 -->
        <div class="item">
          <div class="head">品牌：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="brand in subCategoryData.brands"
              :key="brand.id"
              @click="toggleBrand(brand)"
              >{{ brand.name }}</a
            >
          </div>
        </div>
        <!-- 规格 -->
        <div
          class="item"
          v-for="item in subCategoryData.saleProperties"
          :key="item.id"
        >
          <div class="head">{{ item.name }}：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="i in item.properties"
              :key="i.id"
              :class="{ active: i.selected }"
              @click="toggleSelected(i, item)"
            >
              {{ i.name }}
            </a>
          </div>
        </div>
      </div>

      <div class="list-container">
        <!-- 排序区域 -->
        <div class="sub-sort">
          <div class="sort">
            <a
              v-for="item in curSortTypeList"
              :key="item.id"
              href="javascript:;"
              @click="toggleSort(item.type)"
              :class="{ active: curSortType === item.type }"
              >{{ item.name }}</a>
          </div>
        </div>
      </div>
      <!-- 商品列表 -->
      <div class="sort-list-container">
        <HomeGoods v-for="good in goodsList" :key="good.id" :good="good" />
      </div>

      <!-- 底部加载更多组件 -->
      <XtxInfiniteLoad @load="loadNext" :isFinished="isFinished" :isLoading="isLoading"/>
    </div>
  </div>
</template>

<script>
import { useSubCategoryFilter } from './service/useSubCategoryFilter'
import { useGoodsList } from './service/useGoodsList'
import { useToggleFilterData } from './service/useToggleFilterData'
// 引入商品模块组件
import HomeGoods from '@/views/Home/components/HomeGoods.vue'
export default {
  name: 'SubCategory',
  components: {
    HomeGoods
  },
  setup () {
    // 二级类目
    const { subCategoryDataRef } = useSubCategoryFilter()
    const {
      goodsListRef,
      reqParams,
      loadGoodsList
    } = useGoodsList()
    const {
      toggleSelected,
      curSortTypeListRef,
      curSortTypeRef,
      toggleSort,
      toggleBrand,
      loadNext,
      isFinishedRef,
      isLoadingRef
    } = useToggleFilterData(reqParams, loadGoodsList, goodsListRef)

    // 切换属性规格 如果当前点击的是重复的类别 就已最后一次点击的为主  如果不是重复的类别 就增加一项
    // 思路: 先不管重复逻辑 直接网络添加 -> 再做额外的重复处理

    return {
      subCategoryData: subCategoryDataRef,
      toggleSelected,
      toggleSort,
      curSortType: curSortTypeRef,
      curSortTypeList: curSortTypeListRef,
      goodsList: goodsListRef,
      toggleBrand,
      loadNext,
      isFinished: isFinishedRef,
      isLoading: isLoadingRef
    }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
// 排序区域
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
.list-container {
  margin-top: 20px;
  padding: 0 20px;
  background: #fff;
}

.sort-list-container {
    display: flex;
    flex-wrap: wrap;
 }
</style>
